﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace XamlaApp.ViewModels
{
    [DataContract]
    public class BorderViewModel : PanelViewModel
    {
        Border b = new Border();

        private Thickness _borderThickness = new Thickness(1);
        [DataMember]
        public Thickness BorderThickness
        {
            get { return _borderThickness; }
            set
            {
                _borderThickness = value;
                PropertyChanging("BorderThickness");
            }
        }

        private CornerRadius _cornerRadius = new CornerRadius(0);
        [DataMember]
        public CornerRadius CornerRadius
        {
            get { return _cornerRadius; }
            set
            {
                _cornerRadius = value;
                PropertyChanging("CornerRadius");
            }
        }

        private SolidColorBrush _fill = new SolidColorBrush(Colors.Transparent);
        [IgnoreDataMember]
        public SolidColorBrush FillBrush
        {
            get { return _fill; }
            set
            {
                _fill = value;
                PropertyChanging("FillBrush");
            }
        }

        private SolidColorBrush _strokeBrush = new SolidColorBrush(Colors.Blue);
        [IgnoreDataMember]
        public SolidColorBrush StrokeBrush
        {
            get { return _strokeBrush; }
            set
            {
                _strokeBrush = value;
                PropertyChanging("StrokeBrush");
            }
        }

        internal override XElement GenerateXaml(XElement element)
        {
            var el = new XElement("Border",
                new XAttribute("BorderThickness",BorderThickness),
                new XAttribute("Fill",FillBrush.Color.ToString()),
                new XAttribute("BorderBrush", StrokeBrush.Color.ToString()),
                new XAttribute("CornerRadius", CornerRadius));

            //pass it to base view models for additional attributes
            var finishedXaml = base.GenerateXaml(el);

            //add it to the root xaml
            element.Add(finishedXaml);
            return element;
        }

        #region Json Proxy properties
        // These properties provide a serializable link to other view model properties that cannot be serialized inherently.
        // Sort of like value converters... these are not for databinding

        [DataMember]
        public Color FillBrushProxy
        {
            get { return FillBrush.Color; }
            set
            {
                if (FillBrush == null) FillBrush = new SolidColorBrush();
                FillBrush.Color = value;
            }
        }

        [DataMember]
        public Color StrokeBrushProxy
        {
            get { return StrokeBrush.Color; }
            set
            {
                if (StrokeBrush == null) StrokeBrush = new SolidColorBrush();
                StrokeBrush.Color = value;
            }
        }
        #endregion
    }
}
